<template>
  <div class="DISCList" v-loading="loading">
    <el-button type="primary" size="default" @click="onEdit()"
      >新增DISC</el-button
    >
    <div class="DISCList-content">
      <div
        class="DISCList-content-model"
        v-for="(item, index) in DISCLIST"
        :key="index"
      >
        <div class="DISCList-content-model-one">
          <div>{{ index + 1 }}. {{ item.question.question }}</div>
          <div>
            <el-button type="text" @click="onEdit(item)">编辑</el-button>
            <el-button type="text" style="color: red" @click="onDelete(item.id)"
              >删除</el-button
            >
          </div>
        </div>
        <div class="DISCList-content-model-two">
          <div v-for="(e, idx) in item.question.answer" :key="idx">
            <div style="width: 40px">{{ e.score }}</div>
            <div>
              {{ e.Faq }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <el-dialog
      title="创建DISC"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%"
    >
      <el-form ref="userForm" label-width="100px">
        <el-form-item label="问题" class="myform-item">
          <el-input
            v-model="question"
            placeholder="请输入问题"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="D" class="myform-item">
          <el-input
            v-model="answer[0].Faq"
            placeholder="请输入选项内容"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="I" class="myform-item">
          <el-input
            v-model="answer[1].Faq"
            placeholder="请输入选项内容"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="S" class="myform-item">
          <el-input
            v-model="answer[2].Faq"
            placeholder="请输入选项内容"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="C" class="myform-item">
          <el-input
            v-model="answer[3].Faq"
            placeholder="请输入选项内容"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-button
          type="primary"
          style="width: 100%; margin: 30px 0"
          @click="onAddPostPup()"
          :loading="loading"
          >添加</el-button
        >
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminRecruitmentInformationSelectDISC,
  postAdminRecruitmentInformationGetProfession,
  postAdminRecruitmentInformationAddDISC,
  postAdminRecruitmentInformationDelDISC,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      id: "",
      DISCLIST: [],
      tableData: [],
      question: "",
      answer: [
        {
          Faq: "",
          score: "D",
        },
        {
          Faq: "",
          score: "I",
        },
        {
          Faq: "",
          score: "S",
        },
        {
          Faq: "",
          score: "C",
        },
      ],
      total: 1,
    };
  },
  activated() {
    this.postAdminRecruitmentInformationSelectDISC();
    this.postAdminRecruitmentInformationGetProfession();
  },
  methods: {
    onDelete(id) {
      let that = this;
      let obj = {
        id,
      };
      this.$confirm("确定删除该DISC?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminRecruitmentInformationDelDISC(obj).then((res) => {
            this.postAdminRecruitmentInformationSelectDISC();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleChangeSearch(e) {
      console.log("handleChangeSearch------->", e);
      let arr = JSON.parse(JSON.stringify(e));
      this.form.t_id = arr.pop();
    },
    handleChange(e) {
      console.log("e------->", e);
      let arr = JSON.parse(JSON.stringify(e));
      this.userForm.t_id = arr.pop();
    },
    onPostModel() {
      this.postIds = "";
      this.userForm = {
        t_id: "",
        title: "",
      };
      this.dialogVisible = true;
    },
    // 部门
    postAdminRecruitmentInformationGetProfession() {
      postAdminRecruitmentInformationGetProfession().then((res) => {
        this.postlist = res.data;
      });
    },
    onAddPositionIssue(e) {
      let detail = JSON.stringify(e);
      console.log("detail66666666---->", detail);
      this.$router.push({ name: "HRInterviewIssue", query: { detail } });
    },
    // DISC列表
    async postAdminRecruitmentInformationSelectDISC() {
      this.loading = true;
      postAdminRecruitmentInformationSelectDISC()
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.DISCLIST = res.data;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    someanswer() {
      let { question, answer } = this;
      if (!question) {
        this.$message.warning("请输入问题");
        return false;
      }
      let isanswer = answer.some((e) => !e.Faq);
      if (isanswer) {
        this.$message.warning("请输入选项内容");
        return false;
      }
      let obj;
      if (this.id) {
        obj = {
          id: this.id,
          question: JSON.stringify({
            question,
            answer,
          }),
        };
      } else {
        obj = {
          question: JSON.stringify({
            question,
            answer,
          }),
        };
      }

      return obj;
    },
    // 添加DISC
    onAddPostPup() {
      let that = this;
      let obj = this.someanswer();
      console.log("obj", obj);
      if (obj) {
        this.loading = true;
        postAdminRecruitmentInformationAddDISC(obj).then((res) => {
          this.$message.success("创建成功");
          let form = that.form;
          this.postAdminRecruitmentInformationSelectDISC(form);
          this.dialogVisible = false;
          this.loading = false;
        });
      }
    },
    // 编辑
    onEdit(item) {
      this.id = "";
      this.question = "";
      this.answer = [
        {
          Faq: "",
          score: "D",
        },
        {
          Faq: "",
          score: "I",
        },
        {
          Faq: "",
          score: "S",
        },
        {
          Faq: "",
          score: "C",
        },
      ];
      if (item) {
        this.question = item.question.question;
        this.answer = item.question.answer;
        this.id = item.id;
      }
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.DISCList {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
  &-content {
    margin-top: 40px;
    > div {
      margin-bottom: 20px;
    }
    &-model {
      padding: 0 20px;
      &-one {
        font-size: 18px;
        font-weight: 800;
        display: flex;
        align-items: center;
        justify-content: space-between;

        margin-bottom: 10px;
      }
      &-two {
        border-bottom: 1px solid #eee;
        padding-bottom: 10px;
        > div {
            padding-bottom: 10px;
          display: flex;
          margin-left: 20px;
        }
      }
    }
  }
}
</style>
